//
//  YZLNewsCell.m
//  Car
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLNewsCell.h"
#import "YZLNewsListModel.h"
#import "Macro.h"

@interface YZLNewsCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *descLabel;

@end

@implementation YZLNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = [UIColor grayColor];
        self.dateLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.dateLabel];
        
        self.typeLabel = [[UILabel alloc] init];
        self.typeLabel.textColor = [UIColor grayColor];
        self.typeLabel.font = [UIFont systemFontOfSize:14];
        self.typeLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.typeLabel];
        
        self.descLabel = [[UILabel alloc] init];
        self.descLabel.font = [UIFont systemFontOfSize:15.5];
        self.descLabel.numberOfLines = 0;
        [self.contentView addSubview:self.descLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (!self.isAll) {
        self.titleLabel.frame = CGRectMake(15, 0, kWidth - 30, self.contentView.frame.size.height / 4);
        
        
        self.descLabel.frame = CGRectMake(15, self.titleLabel.frame.size.height, kWidth - 30, self.contentView.frame.size.height / 2);
        
        self.dateLabel.frame = CGRectMake(15, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2, 16);
        self.typeLabel.frame = CGRectMake(kWidth / 2, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2 - 15, 16);
    } else {
        self.titleLabel.frame = CGRectMake(15, 0, kWidth - 30, self.contentView.frame.size.height * 2 / 3);
        self.dateLabel.frame = CGRectMake(15, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2, self.contentView.frame.size.height / 3);
        self.typeLabel.frame = CGRectMake(kWidth / 2, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2 - 15, self.contentView.frame.size.height / 3);
    }

}

- (void)setSearchString:(NSString *)searchString {
    if (_searchString != searchString) {
        _searchString = searchString;
    }
    
    NSMutableAttributedString *arrStr = [[NSMutableAttributedString alloc] initWithString:[[_model.newsTitle stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""]];
    NSRange range = [[[_model.newsTitle stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""] rangeOfString:_searchString];
    [arrStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:17]} range:range];
    
    _titleLabel.attributedText = arrStr;
    
    NSString *decsStr = [[_model.newsDesc stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    NSMutableAttributedString *arrDecsStr = [[NSMutableAttributedString alloc] initWithString: decsStr];
    NSRange decsRange = [decsStr rangeOfString:_searchString];
    
    [arrDecsStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:15]} range:decsRange];
    _descLabel.attributedText = arrDecsStr;
}

- (void)setModel:(YZLNewsListModel *)model {
    if (_model != model) {
        _model = model;
    }
    
    self.dateLabel.text = [[NSString stringWithFormat:@"%@", [NSDate dateWithTimeIntervalSince1970:[model.newsCreateTime integerValue] + 8 * 3600]] substringToIndex:10];
    self.typeLabel.text = model.newsCategory;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
